package com.csvc.controller.dashboard.cmdb;

import java.util.Objects;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csvc.business.cmdb.service.IOracleInfoService;
import com.csvc.commons.response.ResponseResult;
import com.csvc.repository.cmdb.domain.OracleInfo;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author vincentzou
 * @since 2020-12-29
 */
@RestController
@RequestMapping("/oracle-info")
public class OracleInfoController {
	@Resource
	private IOracleInfoService oracleInfoService;

	/**
	 * 新增
	 *
	 * @param oracleInfo {@link OracleInfo}
	 * @return {@link ResponseResult}
	 */
	@PostMapping("create")
	public ResponseResult create(@Valid @RequestBody OracleInfo oracleInfo, BindingResult bindingResult) {
		// 表单验证
		if (bindingResult.hasErrors()) {
			return ResponseResult.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
		}

		// 业务逻辑
		boolean created = oracleInfoService.create(oracleInfo);
		if (created) {
			return ResponseResult.success("创建成功");
		}

		return null;
	}

	/**
	 * 删除
	 *
	 * @param oracleInfoId {@code Long}
	 * @return {@link ResponseResult}
	 */
	@DeleteMapping("remove/{oracleInfoId}")
	public ResponseResult remove(@PathVariable Long oracleInfoId) {
		// 业务逻辑
		boolean deleted = oracleInfoService.remove(oracleInfoId);
		if (deleted) {
			return ResponseResult.success("删除成功");
		}

		return null;
	}

	/**
	 * 修改
	 *
	 * @param oracleInfo {@link OracleInfo}
	 * @return {@link ResponseResult}
	 */
	@PutMapping("update")
	public ResponseResult update(@Valid @RequestBody OracleInfo oracleInfo, BindingResult bindingResult) {
		// 表单验证
		if (bindingResult.hasErrors()) {
			return ResponseResult.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
		}

		// 业务逻辑
		boolean updated = oracleInfoService.update(oracleInfo);
		if (updated) {
			return ResponseResult.success("编辑成功");
		}

		return null;
	}

	/**
	 * 获取
	 *
	 * @param oracleInfoId {@code Long}
	 * @return {@link ResponseResult}
	 */
	@GetMapping("get/{oracleInfoId}")
	public ResponseResult get(@PathVariable Long oracleInfoId) {
		OracleInfo oracleInfo = oracleInfoService.get(oracleInfoId);
		return ResponseResult.success(oracleInfo);
	}

	/**
	 * 分页
	 *
	 * @param current {@code int} 页码
	 * @param size    {@code int} 笔数
	 * @return {@link ResponseResult}
	 */
	@GetMapping("page")
	public ResponseResult page(@RequestParam int current, @RequestParam int size,
			@ModelAttribute OracleInfo oracleInfo) {
		IPage<OracleInfo> page = oracleInfoService.page(current, size, oracleInfo);
		return ResponseResult.success(page);
	}

}
